import fs from 'fs/promises'
import path from 'path'

//定义文件位置
const dataFilePath = path.join('.', 'data', 'weight-data.json');

//写入一条新记录
async function addNewRecord(newRecord) {
    //读取原文件数据
    let file_data = fs.readFile(dataFilePath, 'utf-8');
    let jsonFileData = JSON.parse(await file_data);
    //将新数据追加的变量中
    newRecord.weight = Number(newRecord.weight);
    jsonFileData.push(newRecord);
    //写入数据
    return fs.writeFile(dataFilePath, JSON.stringify(jsonFileData), 'utf-8');
}

export default {
    addNewRecord
}